package demo.zyy.com.dreamstudio.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import demo.zyy.com.dreamstudio.R;
import demo.zyy.com.dreamstudio.adapter.CategoryFgAdapter;
import demo.zyy.com.dreamstudio.base.BaseFragment;
import demo.zyy.com.dreamstudio.bean.Drink;

/**
 * Created by Administrator on 2017/8/15.
 */

public class CategoryFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    @BindView(R.id.categort_list)
    ListView categortList;
    @BindView(R.id.categort_commit)
    Button categortCommit;
    @BindView(R.id.categort_order)
    Button categortOrder;
    @BindView(R.id.categort_allselect)
    Button categortAllselect;
    @BindView(R.id.categort_inverse)
    Button categortInverse;
    @BindView(R.id.categort_ll)
    LinearLayout categortLl;
    private ArrayList<Drink> drinks = new ArrayList<Drink>();
    private CategoryFgAdapter adapter;
    private CheckBox checkBox;
    private int checkNum;//记录选中的条目数量
    private ArrayList<String> list;
    private boolean isShow = false;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                show();
            } else if (msg.what == 0) {
                dismiss();
            }
        }
    };

    private void dismiss() {
        adapter = new CategoryFgAdapter(drinks, getActivity().getApplicationContext(), false);
        categortList.setAdapter(adapter);
        isShow = false;
        categortInverse.setVisibility(View.GONE);
        categortAllselect.setVisibility(View.GONE);

    }

    private void show() {
        adapter = new CategoryFgAdapter(drinks, getActivity().getApplicationContext(), true);
        categortList.setAdapter(adapter);
        isShow = true;
        categortInverse.setVisibility(View.VISIBLE);
        categortAllselect.setVisibility(View.VISIBLE);
    }

    @Override
    protected int loadXML() {
        return R.layout.categort_fragment_layout;
    }

    @Override
    protected void initView(View view) {

        View view1 = LayoutInflater.from(getContext()).inflate(R.layout.categort_fragment_item, null);
        list = new ArrayList<String>();

        if (isShow) {
            categortOrder.setText("取消");
            categortInverse.setVisibility(View.VISIBLE);
            categortAllselect.setVisibility(View.VISIBLE);
        } else {
            categortOrder.setText("点餐");
            categortAllselect.setVisibility(View.GONE);
            categortInverse.setVisibility(View.GONE);
        }
        categortAllselect.setOnClickListener(this);
        categortInverse.setOnClickListener(this);
        categortOrder.setOnClickListener(this);
        categortCommit.setOnClickListener(this);
        initdate();

        adapter = new CategoryFgAdapter(drinks, getActivity().getApplicationContext(), false);
        isShow = false;
        categortList.setAdapter(adapter);
//        categortList.setOnItemSelectedListener(this);


    }
    private void initdate(){
        Class cls=R.mipmap.class;//反射
        try {
            drinks.add(new Drink(cls.getDeclaredField("btn_off").getInt(null),"香蕉","10"));
            drinks.add(new Drink(cls.getDeclaredField("btn_off").getInt(null),"苹果","20"));
            drinks.add(new Drink(cls.getDeclaredField("btn_off").getInt(null),"猕猴桃","30"));
            drinks.add(new Drink(cls.getDeclaredField("btn_off").getInt(null),"牛油果","40"));
            drinks.add(new Drink(cls.getDeclaredField("btn_off").getInt(null),"香梨","50"));
            drinks.add(new Drink(cls.getDeclaredField("btn_off").getInt(null),"黄杏","60"));
            drinks.add(new Drink(cls.getDeclaredField("btn_off").getInt(null),"橘子","70"));
            drinks.add(new Drink(cls.getDeclaredField("btn_off").getInt(null),"葡萄","80"));
            drinks.add(new Drink(cls.getDeclaredField("btn_off").getInt(null),"哈密瓜","90"));
            drinks.add(new Drink(cls.getDeclaredField("btn_off").getInt(null),"芒果","100"));
             for (int i=0;i<drinks.size();i++){
                 list.add("data"+""+i);
             }
        }catch (Exception e){
            e.printStackTrace();
        }


    }


    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()){
            case R.id.categort_commit://计算总价
                myPrice();
                break;
            case R.id.categort_order:
                whatToShow();
                break;
            case R.id.categort_allselect:
                selectAll();
                break;
            case R.id.categort_inverse:
                selectNone();
                break;
        }



    }

    private void selectNone() {
        //遍历list的长度，将Adapter中的map值全部设为true
        for (int i=0;i<list.size();i++){
            adapter.getIsSelected().put(i,false);
        }
        //数量设为list的长度
        checkNum=list.size();
        //刷新listview和textview的显示
        adapter.notifyDataSetChanged();

    }

    private void selectAll() {
     //遍历list的长度，将Adapter中的map值全部设为true
        for (int i=0;i<list.size();i++){
            adapter.getIsSelected().put(i,true);
        }
        //数量设为list的长度
        checkNum=list.size();
        //刷新listview和textview的显示
        adapter.notifyDataSetChanged();
    }
    //决定第二个按钮显示
    private void whatToShow() {
     if(isShow){
         Message message=Message.obtain();
         message.what=0;
         handler.sendMessage(message);
         categortOrder.setText("点餐");
     }else {
         Message message=Message.obtain();
         message.what=1;
         handler.sendMessage(message);
         categortOrder.setText("取消");
     }

    }

    private void myPrice() {
        HashMap<Integer,Boolean>map=adapter.getIsSelected();
        String str="";
        int money=0;
        for (int i=0;i<map.size();i++){
            if(map.get(i)){
                str+=(i+"");
                money+=Integer.parseInt(drinks.get(i).getPrice());
            }
        }
        adapter.getIsSelected().get("");

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       //取得viewHolder对象，这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤
        CategoryFgAdapter.CategoryFgHolder holder= (CategoryFgAdapter.CategoryFgHolder) view.getTag();
        //改变checkBox的状态
        holder.categort_itemcheckbok.toggle();
       //将CheckBox的选中状态记录下来
        adapter.getIsSelected().put(position,holder.categort_itemcheckbok.isChecked());
    }


}
